<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px"
      class="top-left">
      <!--      <el-form-item label="父试验类型id" prop="parentId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.parentId"-->
      <!--          placeholder="请输入父试验类型id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label prop="syTypeName">
        <el-input v-model="queryParams.syTypeName" placeholder="请输入试验类型名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label prop="syTypeName">
        <el-select v-model="queryParams.syBigType" clearable placeholder="请选择试验大类" style="width: 100%;">
          <el-option v-for="dict in dict.type.sy_big_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="祖籍列表" prop="ancestors">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ancestors"-->
      <!--          placeholder="请输入祖籍列表"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['base:syType:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>
    <!--    <el-row>-->
    <!--      <el-col>-->
    <!--        <span class="note">*一二级为试验类型，三级及三级以下为试验分类， 新增和修改时，后台需自动计算level</span>-->
    <!--      </el-col>-->
    <!--    </el-row>-->

    <div style="margin-top: 20px;">
      <span class="note">*一二级为试验类型，三级及三级以下为试验分类， 新增和修改时，后台需自动计算level</span>
    </div>

    <el-table border stripe v-if="refreshTable" v-loading="loading" :data="syTypeList" row-key="syTypeId"
      :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <!--      <el-table-column label="父试验类型id" prop="parentId" />-->
      <el-table-column label="试验类型名称" align="left" prop="syTypeName" min-width="180" show-overflow-tooltip />
      <el-table-column label="试验标识" align="center" prop="identification" min-width="120" show-overflow-tooltip />
      <el-table-column label="试验大类" align="center" prop="syBigType" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ syBigTypeFormat(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" min-width="80" />
      <el-table-column label="类型等级" align="center" prop="level" min-width="80" />

      <!--      <el-table-column label="祖籍列表" align="center" prop="ancestors" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['base:syType:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
            v-hasPermi="['base:syType:add']">新增</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['base:syType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改试验类型对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="父试验类型" prop="parentId">
          <treeselect v-model="form.parentId" :disabled="true" :options="syTypeOptions" :normalizer="normalizer"
            placeholder="请选择父试验类型" />
        </el-form-item>
        <el-form-item label="试验类型名称" prop="syTypeName">
          <el-input v-model="form.syTypeName" placeholder="请输入试验类型名称" />
        </el-form-item>
        <el-form-item label="试验大类" prop="syBigType">
          <el-select v-model="form.syBigType" placeholder="请选择试验大类" style="width: 100%;">
            <el-option v-for="dict in dict.type.sy_big_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" type="Number" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="试验标识" prop="identification">
          <el-input v-model="form.identification" placeholder="请输入项目标识" />
        </el-form-item>
        <el-form-item label="是否工地内" prop="innerFlag">
          <el-radio-group v-model="form.innerFlag">
            <el-radio v-for="dict in dict.type.inner_flag" :key="dict.value" :label="dict.value">{{
      dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="类型等级" label-width='120px' prop="level">
          <el-input v-model="form.level" type="Number" placeholder="请输入类型等级" />
        </el-form-item>-->
        <!--        <el-form-item label="祖籍列表" prop="ancestors">-->
        <!--          <el-input v-model="form.ancestors" type="textarea" placeholder="请输入内容" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="small" @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listSyType, getSyType, delSyType, addSyType, updateSyType } from "@/api/base/syType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SyType",
  dicts: ["inner_flag", "sy_big_type"],
  components: {
    Treeselect
  },
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 试验类型表格数据
      syTypeList: [],
      // 试验类型树选项
      syTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: undefined,
        syTypeName: undefined,
        syBigType: undefined,
        ancestors: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        syTypeId: [{ required: true, message: "试验类型id不能为空", trigger: "blur" }],
        parentId: [{ required: true, message: "父试验类型id不能为空", trigger: "blur" }],
        syTypeName: [{ required: true, message: "试验类型名称不能为空", trigger: "blur" }],
        ancestors: [{ required: true, message: "祖籍列表不能为空", trigger: "blur" }],
        delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询试验类型列表 */
    getList () {
      this.loading = true;
      listSyType(this.queryParams).then((response) => {
        this.syTypeList = this.handleTree(response.data, "syTypeId", "parentId");
        this.loading = false;
      });
    },
    /** 转换试验类型数据结构 */
    normalizer (node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.syTypeId,
        label: node.syTypeName,
        children: node.children
      };
    },
    /** 查询试验类型下拉树结构 */
    getTreeselect () {
      listSyType().then((response) => {
        this.syTypeOptions = [];
        const data = { syTypeId: 0, syTypeName: "顶级节点", children: [] };
        data.children = this.handleTree(response.data, "syTypeId", "parentId");
        this.syTypeOptions.push(data);
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        syTypeId: null,
        parentId: null,
        syTypeName: null,
        syBigType: null,
        ancestors: null,
        innerFlag: "1",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.syTypeId) {
        this.form.parentId = row.syTypeId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加试验类型";
    },
    /** 展开/折叠操作 */
    toggleExpandAll () {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.syTypeId;
      }
      getSyType(row.syTypeId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改试验类型";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.syTypeId != null) {
            updateSyType(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addSyType(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      this.$modal
        .confirm('是否确认删除试验类型编号为"' + row.syTypeId + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delSyType(row.syTypeId);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { })
        .finally(() => {
          this.loading = false;
        });
    },
    // 字典翻译
    syBigTypeFormat (row, column) {
      return this.selectDictLabel(this.dict.type.sy_big_type, row.syBigType);
    },
  }
};
</script>

<style scoped>
.note {
  padding-left: 36px;
  height: 40px;
  text-align: left;
  font-weight: 400;
  font-size: 14px;
  color: #f56c6c;
  line-height: 40px;
}
</style>
